package com.smartthinking.qualitystores;

import java.util.ArrayList;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Tab4Fragment extends Fragment {
	
	String saldo_actual, liquede_con, mv_la, mv, pm24, pm12, linea_articulos, linea_prestamos, ultupdt, almacen, monto, tipo, user, pass, edo,
	det, bon, bonnew;
	ListView lista1, lista2, lista3;
	public AdaptadorBusqueda0 adaptadorbusqueda0;
	public AdaptadorBusqueda1 adaptadorbusqueda1;
	public AdaptadorBusqueda2 adaptadorbusqueda2;
	ArrayList<String> listultupdt = new ArrayList<String>();
	ArrayList<String> listalmacen = new ArrayList<String>();
	ArrayList<String> listmonto = new ArrayList<String>();
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//		return inflater.inflate(R.layout.tab_fragment4, null);
		return inflater.inflate(R.layout.tab_fragment2, null);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initView();
	}
	
	private void initView() {
		
		listultupdt.clear();
		listalmacen.clear();
		listmonto.clear();
		
		SharedPreferences preferencias1 = getActivity().getSharedPreferences("prefs", Context.MODE_PRIVATE);
		bon= preferencias1.getString("Bonificacion","1");
		
		SharedPreferences preferencias2 = getActivity().getSharedPreferences("prefs", Context.MODE_PRIVATE);
		bonnew= preferencias2.getString("Bonificacionnew","1");

		if(preferencias1.getString("Bonificacion","1").equals(preferencias2.getString("Bonificacionnew","1"))){
			
			String temporal[]=bon.split("@");
			for (int i=0;i<temporal.length;i++){
				Log.i("temporal", ""+temporal.length);
				
				String temporal0[]=temporal[i].trim().split("%");
				
				ultupdt = temporal0[0].trim();
				Log.i("ultupdt", ""+ultupdt);
				listultupdt.add(ultupdt);
	  		    Log.i("listultupdt",""+listultupdt.size());

				almacen = temporal0[1].trim();
				Log.i("almacen", ""+almacen);
				listalmacen.add(almacen);
	  		    Log.i("listalmacen",""+listalmacen.size());

				monto = temporal0[2].trim();
				Log.i("monto", ""+monto);
				listmonto.add(monto);
	  		    Log.i("listmonto",""+listmonto.size());
				
			}
			
			lista1 = (ListView)getView().findViewById(R.id.listView1);
			    AdaptadorBusqueda0 adapter = new AdaptadorBusqueda0(listultupdt);
			    lista1.setAdapter(adapter);
			    adapter.notifyDataSetChanged();
			    
			lista2 = (ListView)getView().findViewById(R.id.listView2);
			    AdaptadorBusqueda1 adapter1 = new AdaptadorBusqueda1(listalmacen);
			    lista2.setAdapter(adapter1);
			    adapter1.notifyDataSetChanged();
			    
			lista3 = (ListView)getView().findViewById(R.id.listView3); 
			    AdaptadorBusqueda2 adapter2 = new AdaptadorBusqueda2(listmonto);
			    lista3.setAdapter(adapter2);
			    adapter2.notifyDataSetChanged();
			
		}else{
			
			String temporal[]=bonnew.split("@");
			for (int i=0;i<temporal.length;i++){
				Log.i("temporal", ""+temporal.length);
				
				String temporal0[]=temporal[i].trim().split("%");
				
				ultupdt = temporal0[0].trim();
				Log.i("ultupdt", ""+ultupdt);
				listultupdt.add(ultupdt);
	  		    Log.i("listultupdt",""+listultupdt.size());

				almacen = temporal0[1].trim();
				Log.i("almacen", ""+almacen);
				listalmacen.add(almacen);
	  		    Log.i("listalmacen",""+listalmacen.size());

				monto = temporal0[2].trim();
				Log.i("monto", ""+monto);
				listmonto.add(monto);
	  		    Log.i("listmonto",""+listmonto.size());
				
			}
			
			lista1 = (ListView)getView().findViewById(R.id.listView1);
			    AdaptadorBusqueda0 adapter = new AdaptadorBusqueda0(listultupdt);
			    lista1.setAdapter(adapter);
			    adapter.notifyDataSetChanged();
			    
			lista2 = (ListView)getView().findViewById(R.id.listView2);
			    AdaptadorBusqueda1 adapter1 = new AdaptadorBusqueda1(listalmacen);
			    lista2.setAdapter(adapter1);
			    adapter1.notifyDataSetChanged();
			    
			lista3 = (ListView)getView().findViewById(R.id.listView3); 
			    AdaptadorBusqueda2 adapter2 = new AdaptadorBusqueda2(listmonto);
			    lista3.setAdapter(adapter2);
			    adapter2.notifyDataSetChanged();
			
		}
	}
		
//		lista1 = (ListView)getView().findViewById(R.id.listView1);
//		
//		String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
//		        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
//		        "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
//		        "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
//		        "Android", "iPhone", "WindowsMobile", "Android", "iPhone", "WindowsMobile",
//		        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
//		        "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
//		        "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
//		        "Android", "iPhone", "WindowsMobile" };
//
//		    final ArrayList<String> list = new ArrayList<String>();
//		    for (int i = 0; i < values.length; ++i) {
//		      list.add(values[i]);
//		    }
//		    AdaptadorBusqueda0 adapter = new AdaptadorBusqueda0(list);
//		    lista1.setAdapter(adapter);
//		    
//		    
//		lista2 = (ListView)getView().findViewById(R.id.listView2);
//		
////		String[] values1 = new String[] { almacen};
//		String[] values1 = new String[] { "Android", "iPhone", "WindowsMobile",
//		        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
//		        "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
//		        "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
//		        "Android", "iPhone", "WindowsMobile", "Android", "iPhone", "WindowsMobile",
//		        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
//		        "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
//		        "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
//		        "Android", "iPhone", "WindowsMobile"};
//
//		    final ArrayList<String> list1 = new ArrayList<String>();
//		    for (int i = 0; i < values1.length; ++i) {
//		      list1.add(values1[i]);
//		    }
//		    AdaptadorBusqueda1 adapter1 = new AdaptadorBusqueda1(list1);
//		    lista2.setAdapter(adapter1);
//		    
//		    
//		lista3 = (ListView)getView().findViewById(R.id.listView3); 
//		
////		String[] values2 = new String[] { monto };
//		String[] values2 = new String[] { "Android", "iPhone", "WindowsMobile",
//		        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
//		        "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
//		        "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
//		        "Android", "iPhone", "WindowsMobile","Android", "iPhone", "WindowsMobile",
//		        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
//		        "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
//		        "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
//		        "Android", "iPhone", "WindowsMobile" };
//
//		    final ArrayList<String> list2 = new ArrayList<String>();
//		    for (int i = 0; i < values2.length; ++i) {
//		      list2.add(values2[i]);
//		    }
//		    AdaptadorBusqueda2 adapter2 = new AdaptadorBusqueda2(list2);
//		    lista3.setAdapter(adapter2);
//		
//	}
	
	
	public class AdaptadorBusqueda0 extends BaseAdapter {

		private final ArrayList<String> ListaDatos;

		public AdaptadorBusqueda0(ArrayList<String> list) {
			// TODO Auto-generated constructor stub
			this.ListaDatos = list;
		}

		@Override
		public int getCount() {
			return ListaDatos.size();
		}

		@Override
		public Object getItem(int arg0) {
			return ListaDatos.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(final int arg0, View arg1, ViewGroup arg2) {
			View view = arg1;
			Holder holder;
			if(view == null){
				LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.listafecha, arg2, false);
				holder = new Holder();
				holder.nombre = (TextView) view.findViewById(R.id.textView1);
				view.setTag(holder);
			}
			else{
				holder = (Holder) view.getTag();

			}
			holder.nombre.setText(ListaDatos.get(arg0));
			return view;
		}
	}

	public static class Holder{
		public TextView nombre;
	}
	
	
	
	public class AdaptadorBusqueda1 extends BaseAdapter {

		private final ArrayList<String> ListaDatos;

		public AdaptadorBusqueda1(ArrayList<String> list) {
			// TODO Auto-generated constructor stub
			this.ListaDatos = list;
		}

		@Override
		public int getCount() {
			return ListaDatos.size();
		}

		@Override
		public Object getItem(int arg0) {
			return ListaDatos.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(final int arg0, View arg1, ViewGroup arg2) {
			View view = arg1;
			Holder holder;
			if(view == null){
				LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.listafecha, arg2, false);
				holder = new Holder();
				holder.nombre = (TextView) view.findViewById(R.id.textView1);
				view.setTag(holder);
			}
			else{
				holder = (Holder) view.getTag();

			}
			holder.nombre.setText(ListaDatos.get(arg0));
			return view;
		}
	}

	public static class Holder1{
		public TextView nombre;
	}
	
	
	public class AdaptadorBusqueda2 extends BaseAdapter {

		private final ArrayList<String> ListaDatos;

		public AdaptadorBusqueda2(ArrayList<String> list) {
			// TODO Auto-generated constructor stub
			this.ListaDatos = list;
		}

		@Override
		public int getCount() {
			return ListaDatos.size();
		}

		@Override
		public Object getItem(int arg0) {
			return ListaDatos.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(final int arg0, View arg1, ViewGroup arg2) {
			View view = arg1;
			Holder holder;
			if(view == null){
				LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.listafecha, arg2, false);
				holder = new Holder();
				holder.nombre = (TextView) view.findViewById(R.id.textView1);
				view.setTag(holder);
			}
			else{
				holder = (Holder) view.getTag();

			}
			holder.nombre.setText(ListaDatos.get(arg0));
			return view;
		}
	}

	public static class Holder2{
		public TextView nombre;
	
	}
	
//	private void replaceFragment() {
//		((BaseContainerFragment)getParentFragment()).replaceFragment(new Tab1AddOnFragment(), true);
//	}
	
}
